# -*- encoding=utf8 -*-
        
__author__ = "Bryan.Chen"
__desc__=""
        
from airtest.core.api import *
        
auto_setup(__file__)
        
from poco.drivers.std import StdPoco
poco = StdPoco()
        
using("../FlowTest.air")
import FlowTest

import random
        
class Club_CreateClub(FlowTest.BaseTestCase):
    def __init__(self):
        print("%s __init__" % (__file__))
        
    def runTest(self, args):
        print("%s runTest" % (__file__))
        poco("ClubHomePageLayer").wait_for_appearance()

        btnMyJoinPress = poco("my_join_press")
        btnMyJoinNor = poco("my_join_nor")
        btnMyCreateNor = poco("my_create_nor")
        btnMyCreatePress = poco("my_create_press")
        if btnMyJoinPress.exists():
            self.clickButton(btnMyCreateNor)

        if btnMyCreatePress.exists():
            # 当前已经切换到创建面板

            btnCreateClub = poco("club_add_item")
            if not btnCreateClub.exists():
                # 已经创建了三个俱乐部了，不能再创建了
                return False

            self.clickButton(btnCreateClub)

            tipsDialog = poco("LBTipsLayer")
            if tipsDialog.exists():
                txtInfo = tipsDialog.offspring("info")
                if txtInfo == "您还不是代理，不能创建俱乐部":
                    assert_equal(True, True, "不是代理，不能创建俱乐部") 
                    btnOK = tipsDialog.offspring("ok_btn_1")
                if btnOK.exists():
                    self.clickButton(btnOK)
            else:
                createLayer = poco("ClubCreatePageLayer")
                if not createLayer.exists(): 
                    return False

                btnClubIcon = createLayer.offspring("signet_icon")
                self.clickButton(btnClubIcon)

                btnOK = poco("ClubIconChooseLayer").offspring("ok_btn")
                if btnOK.exists():
                    self.clickButton(btnOK)

                strClubName = "AT_%s" % random.randint(1, 1000)
                txtClubName = createLayer.offspring("club_name")
                self.clickButton(txtClubName)
                text(strClubName)
                
                txtClubDesc = createLayer.offspring("club_desc")
                self.clickButton(txtClubDesc)
                text(strClubName)

                btnSelectGame = createLayer.offspring("playWay_change_btn")
                self.clickButton(btnSelectGame)

                chooseGameLayer = poco("ClubGameChoosePageLayer")
                gameList = chooseGameLayer.offspring("submj_game_list")
                gameItems = gameList.offspring("subgame_item_panel")
                isSelectGame = False
                for item in gameItems:
                    txtName = item.offspring("sub_name").get_text()
                    if txtName == "血流换三张":
                        self.clickButton(item)
                        isSelectGame = True
                        break

                if isSelectGame:
                    playWayLayer = chooseGameLayer.offspring("ClubPlayWaySetPageLayer")
                    if not playWayLayer.exists():
                        return False
                    
                    btnFinish = playWayLayer.offspring("on_btn")
                    self.clickButton(btnFinish)

                    btnFinishCreate = createLayer.offspring("club_create_btn")
                    self.clickButton(btnFinishCreate)
                else: 
                    btnBack = createLayer.offspring("back_btn")
                    self.clickButton(btnBack)

        return True
        
_instance = Club_CreateClub()
runTest = _instance.runTest
